package freedom.soluttion.app.restaurante;

import java.util.ArrayList;
import java.util.HashMap;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import freedom.solution.app.restaurante.datatypes.DtoUsuario;
import freedom.solution.app.restaurante.service.Conexion;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;



public class DetallePedidoActivity extends Activity {
	// All static variables
//	static final String URL = "http://api.androidhive.info/music/music.xml";
	static final String KEY_ID = "id";
	static final String KEY_THUMB_URL = "thumb_url";
	static final String KEY_NOMBRE="nombre";
	static final String KEY_DESCRIPCION="descripcion";
	static final String KEY_CANTIDAD="cantidad";
	static final String KEY_PRECIO="precio_unitario";
	private ProgressDialog pDialog;
	ArrayList<HashMap<String, String>> detallePedido=new ArrayList<HashMap<String,String>>();
	ListView list;
    LazyAdapter adapter;
    int id_mesa;
    
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_menu);
		
		Bundle bundle = getIntent().getExtras();
		id_mesa=Integer.parseInt((bundle.getString("id_mesa")));
		detallePedido= new ArrayList<HashMap<String,String>>();
		  adapter=new LazyAdapter(this, detallePedido);
		new cargarDetallePedido().execute();
		
        setTitle("Detalle Pedido " + id_mesa );
		
	}	
	
	  class cargarDetallePedido extends AsyncTask<String, String, String> {
			 
	        /**
	         * Before starting background thread Show Progress Dialog
	         * */
	        @Override
	        protected void onPreExecute() {
	            super.onPreExecute();
	            pDialog = new ProgressDialog(DetallePedidoActivity.this);
	            pDialog.setMessage("Cargando Detalle Pedido ...");
	            pDialog.setIndeterminate(false);
	            pDialog.setCancelable(false);
	            pDialog.show();
	        }
	 
	        /**
	         * getting Albums JSON
	         * */
	        protected String doInBackground(String... args) {
	            // Building Parameters
	        	Conexion odaocliente=new Conexion();
	    		odaocliente.ObtenerDetallePedidoXMesa();
	    		
	    	    
	    		SoapObject request = new SoapObject(odaocliente.NAMESPACE, odaocliente.METHOD_NAME);
	    		request.addProperty("id_mesa",id_mesa );  

	    		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	    		envelope.dotNet = true; 
	    		envelope.setOutputSoapObject(request);
	    		HttpTransportSE androidHttpTransport = new HttpTransportSE(odaocliente.URL);
	    		androidHttpTransport.debug=true;

	    		try 
	    		{
	    			androidHttpTransport.call(odaocliente.SOAP_ACTION, envelope);
	    			
	    			SoapObject resSoap =(SoapObject)envelope.getResponse();
	    			
	    			String[] obtenerEstado = new String[resSoap.getPropertyCount()];
	    			
	    			for (int i = 0; i < obtenerEstado.length; i++) 
	    			{
	    		           SoapObject ic = (SoapObject)resSoap.getProperty(i);
	    		            
	    		         
	    		           String nombre = ic.getProperty(0).toString();
	    		           String descripcion= ic.getProperty(1).toString();
	    		           String foto = ic.getProperty(2).toString();
	    		           String ingredientes = ic.getProperty(3).toString();
	    		           String cantidad= ic.getProperty(4).toString();
	    		           String precio_unitario = ic.getProperty(5).toString();
	    		           String id_detalle_pedido = ic.getProperty(6).toString();
	    		           HashMap<String, String> map = new HashMap<String, String>();
	    					
	    				 map.put(KEY_NOMBRE,nombre );
	                        map.put(KEY_DESCRIPCION, descripcion);
	                      map.put(KEY_THUMB_URL, foto);
	                      map.put(KEY_PRECIO, precio_unitario);
	    			
	    					
	    					
	    					detallePedido.add(map);
	    		    }
	    		
	    		    
	    		        
	    			
	    		} 
	    		catch (Exception e) 
	    		{
	    			//Toast.makeText(this, "No existe categorias", Toast.LENGTH_LONG).show();
	    		} 
	            return null;
	        }
	        
	     
	 
	        /**
	         * After completing background task Dismiss the progress dialog
	         * **/
	        protected void onPostExecute(String file_url) {
	            // dismiss the dialog after getting all albums
	            pDialog.dismiss();
	            // updating UI from Background Thread
	            runOnUiThread(new Runnable() {
	                public void run() {
	                    /**
	                     * Updating parsed JSON data into ListView
	                     * */
	                	list=(ListView)findViewById(R.id.list);
	            		
	            		// Getting adapter by passing xml data ArrayList
	                          
	                    list.setAdapter(adapter);

	                }
	            });
	 
	        }
	 
	    }
}